using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Rendering;

public class WinPanel : MonoBehaviour
{
    public static WinPanel Instance = null;

    [SerializeField] private GameObject[] _stars;
    private Transform _rewardParent;
    private Transform _panel;

    private int _starCount = 3;
    private List<GameObject> _rewardGos = new List<GameObject>();

    public void Init()
    {
        if (Instance == null) Instance = this;

        _panel = transform.Find("Panel");
        _rewardParent = transform.Find("Panel/RewardContent");
    }

    public void OnNextBtnClick()
    {
        GameManager.Instance.SetPanelState(PanelState.Mission);
        GameManager.Instance.PlayBtnClickAudio();
        GameManager.Instance.NextMission();
    }

    public void OnDoubleBtnClick()
    {
        GameManager.Instance.PlayBtnClickAudio();

        AdManager.ShowVideo(() => { GameManager.Instance.AddCoin(rewardMissionEarn + rewardAccuracy + rewardQuality + rewardTime); });
        // AndroidTool.single.CallAdEvent(AndroidToolEnum.OnAdd,
        //     () => { GameManager.Instance.AddCoin(rewardMissionEarn + rewardAccuracy + rewardQuality + rewardTime); });
    }

    public void OnResumeBtnClick()
    {
        GameManager.Instance.PlayBtnClickAudio();
        GameManager.Instance.UnlockMission(GameManager.Instance.MissionIndex + 1);
        GameManager.Instance.LoadScene(Constants.MainScene);
    }

    public void OnMainMenuBtnClick()
    {
        GameManager.Instance.PlayBtnClickAudio();
        GameManager.Instance.UnlockMission(GameManager.Instance.MissionIndex += 1);
        GameManager.Instance.LoadScene(Constants.StartScene);
    }

    public void SetPanelActive(bool isActive)
    {
        gameObject.SetActive(isActive);

        if (isActive)
        {
            AdManager.ShowNative();
            // AndroidTool.single.Ad2Func();
            _panel.localScale = Vector3.zero;
            _panel.DOScale(Vector3.one, 0.8f).SetEase(Ease.OutElastic);

            for (int i = 0; i < _stars.Length; i++)
            {
                _stars[i].gameObject.SetActive(false);
            }

            GameManager.Instance.StopAudio();
            GameManager.Instance.AddCoin(GameManager.Instance.GetRewardMissionEarn());
            GameManager.Instance.AddCoin(GameManager.Instance.GetRewardAccuracy());
            GameManager.Instance.AddCoin(GameManager.Instance.GetRewardQuality());
            GameManager.Instance.AddCoin(GameManager.Instance.GetRewardTime());

            StartCoroutine(ShowStarsCo());
        }
        else
        {
            ObjectPool.GetInstance().RecycleGo(_rewardGos);
        }
    }

    private int rewardMissionEarn, rewardAccuracy, rewardQuality, rewardTime;

    IEnumerator ShowStarsCo()
    {
        for (int i = 0; i < _starCount; i++)
        {
            yield return new WaitForSeconds(0.5f);
            _stars[i].gameObject.SetActive(true);
        }

        //Earn
        yield return new WaitForSeconds(1);
        GameObject go_1 = ObjectPool.GetInstance().SpawnObject(Constants.PathReward, _rewardParent);
        Reward reward_1 = go_1.GetComponent<Reward>();
        rewardMissionEarn = GameManager.Instance.GetRewardMissionEarn();
        reward_1.SetReward("任务奖励", rewardMissionEarn, Constants.PathMissonRewardIcon);
        _rewardGos.Add(go_1);

        //Accuracy
        yield return new WaitForSeconds(1);
        GameObject go_2 = ObjectPool.GetInstance().SpawnObject(Constants.PathReward, _rewardParent);
        Reward reward_2 = go_2.GetComponent<Reward>();
        rewardAccuracy = GameManager.Instance.GetRewardAccuracy();
        reward_2.SetReward("准确性", rewardAccuracy, Constants.PathMissonAccuracyIcon);
        _rewardGos.Add(go_2);

        //Quality
        yield return new WaitForSeconds(1);
        GameObject go_3 = ObjectPool.GetInstance().SpawnObject(Constants.PathReward, _rewardParent);
        Reward reward_3 = go_3.GetComponent<Reward>();
        rewardQuality = GameManager.Instance.GetRewardQuality();
        reward_3.SetReward("清洗质量", rewardQuality, Constants.PathMissonQualityIcon);
        _rewardGos.Add(go_3);

        //Time
        yield return new WaitForSeconds(1);
        GameObject go_4 = ObjectPool.GetInstance().SpawnObject(Constants.PathReward, _rewardParent);
        Reward reward_4 = go_4.GetComponent<Reward>();
        rewardTime = GameManager.Instance.GetRewardTime();
        reward_4.SetReward("时间奖励", rewardTime, Constants.PathMissonTimeIcon);
        _rewardGos.Add(go_4);
    }
}